/**
 *LogFrame.java
 *@anthor Daniel Liu
 *2006-4-25 20:52:58
 **/
package org.lex.utils.logging.handler;

import java.awt.BorderLayout;

import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;

import org.lex.awt.Location;
import org.lex.utils.Streamable;
import org.lex.utils.logging.handler.action.AlwaysOnTopAction;
import org.lex.utils.logging.handler.action.ExitAction;
import org.lex.utils.logging.handler.action.HideBorderAction;


public class LoggerMainFrame extends JFrame {
	private JPanel pane = null;
	private MainMenuBar mainMenuBar = null;
	private JTabbedPane tabPane = null;
	private JPopupMenu defaultPopupMenu = null;

	public LoggerMainFrame() {
		super();
		this.setAlwaysOnTop(false);
		initialize();
	}

	public Streamable addTab(String name) {
		OutputableTabPane outputableTabPane = new OutputableTabPane();
		outputableTabPane.setComponentPopupMenu(this.getDefaultPopupMenu());
		this.getTabPane().addTab(name, outputableTabPane);
		this.getTabPane().setSelectedComponent(outputableTabPane);
		return outputableTabPane;
	}

	public void exit() {
		this.setVisible(false);
		this.dispose();
	}

	// ===================================================

	private void initialize() {
		this.setJMenuBar(getMainMenuBar());
		this.setContentPane(getPane());
		this.setSize(520, 460);
		Location.moveTo(this, Location.SOUTH_WEST);
		this.setUndecorated(true);
		this.setTitle("Log Viewer");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel(new BorderLayout());
			pane.setComponentPopupMenu(this.getDefaultPopupMenu());
			pane.add(getTabPane(), java.awt.BorderLayout.CENTER);
		}
		return pane;
	}

	private MainMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new MainMenuBar(this);
		}
		return mainMenuBar;
	}

	JTabbedPane getTabPane() {
		if (tabPane == null) {
			tabPane = new JTabbedPane();
			tabPane.setInheritsPopupMenu(true);
			tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		}
		return tabPane;
	}

	private JPopupMenu getDefaultPopupMenu() {
		if (null == this.defaultPopupMenu)
			this.defaultPopupMenu = new DefaultPopupMenu(this);
		return defaultPopupMenu;
	}

	/*
	 * Action Area
	 */
	private AlwaysOnTopAction alwaysOnTopAction = null;

	Action getAlwaysOnTopAction() {
		if (null == this.alwaysOnTopAction)
			this.alwaysOnTopAction = new AlwaysOnTopAction(this);
		return this.alwaysOnTopAction;
	}

	private HideBorderAction hideBorderAction = null;

	Action getHideBorderAction() {
		if (null == this.hideBorderAction)
			this.hideBorderAction = new HideBorderAction(this);
		return this.hideBorderAction;
	}

	private ExitAction exitAction = null;

	Action getExitAction() {
		if (null == this.exitAction)
			this.exitAction = new ExitAction();
		return this.exitAction;
	}
}
